import Common from "../Common";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Tree extends Common {

    protected level = 1;

    set setLevel(val) {
        this.level = val
    }

    get getLevel() {
        return this.level
    }

    /**
     * 浇水
     * @param shuiHuBar 浇水工具按钮（在浇水过程中做隐藏、显示处理）
     * @param callback
     */
    async onJiaoShui(shuiHuBar: cc.Node, callback: Function) {
        await this.onBtnClick()
        callback(true)
        const shuihu = this.node.getChildByName('shuihu');
        const animation = shuihu.getComponent(cc.Animation)

        // 隐藏水壶按钮
        await this.opacityTween(shuiHuBar, false);
        await this.opacityTween(shuihu);
        // 实现浇水动作
        animation.on(cc.Animation.EventType.FINISHED, async (type: string, state: cc.AnimationState) => {
            // 显示水滴并且开启动画
            const shuiDi = shuihu.getChildByName('water')
            await this.opacityTween(shuiDi);
            let count = 1;
            animation.on(cc.Animation.EventType.LASTFRAME, (type: string, state: cc.AnimationState) => {
                count++;
                if (count > 2) { // 浇水三次
                    this.opacityTween(shuiHuBar);
                    animation.stop()
                    animation.setCurrentTime(0);
                    shuihu.skewX = 0
                    shuihu.skewY = 0
                    this.opacityTween(shuiDi, false);
                    this.opacityTween(shuihu, false).then(_ => {
                        return this.onComplete('watering', callback)
                    });

                }
            })
            animation.play('shuidi')
        })
        animation.play('jiaoshui');

    }

    showChong(active = false) {
        const children = this.node.children
        for (let item of children) {
            if (item.active !== active && item.name.indexOf('chong') === 0) {
                item.active = active
            }
        }
    }

    /**
     * 施肥
     * @param feiLiaoBar 施肥工具按钮（在施肥过程中做隐藏、显示处理）
     * @param callback
     */
    async onShiFei(feiLiaoBar: cc.Node, callback: Function) {
        this.onBtnClick()
        callback(true)
        const shuihu = this.node.getChildByName('feil1');
        const animation = shuihu.getComponent(cc.Animation)

        // 隐藏水壶按钮
        await this.opacityTween(feiLiaoBar, false);
        await this.opacityTween(shuihu);
        // 实现浇水动作
        animation.on(cc.Animation.EventType.FINISHED, async (type: string, state: cc.AnimationState) => {
            // 显示水滴并且开启动画
            const shuiDi = shuihu.getChildByName('feil3')
            await this.opacityTween(shuiDi);
            let count = 1;
            animation.on(cc.Animation.EventType.FINISHED, (type: string, state: cc.AnimationState) => {
                count++;
                if (count > 2) { // 浇水三次
                    this.opacityTween(feiLiaoBar);
                    animation.stop()
                    animation.setCurrentTime(0);
                    shuihu.skewX = 0
                    shuihu.skewY = 0
                    this.opacityTween(shuiDi, false);
                    this.opacityTween(shuihu, false).then(_ => {
                        return this.onComplete('fertilize', callback)
                    });
                }
            })
            animation.play('huafei')
        })
        animation.play('shifei');

    }

    async onChuChong(chuChongBar: cc.Node, callback: Function) {
        this.onBtnClick()
        callback(true)
        await this.opacityTween(chuChongBar, false);
        // 树上的驱虫剂
        const quchongNode = this.node.getChildByName('quchong')
        await this.opacityTween(quchongNode);
        const children = this.node.children
        for (let item of children) {
            if (item.name.indexOf('chong') === 0) {
                await this._juChong(item, quchongNode)
            }
        }
        this.opacityTween(quchongNode, false);
        await this.opacityTween(chuChongBar);
        this.onComplete('deworming', callback)
    }

    private async _juChong(chongNode: cc.Node, quChongNode: cc.Node) {
        return new Promise((resolve) => {
            let x = chongNode.x + 58
            let y = chongNode.y - 10
            cc.tween(quChongNode).to(1, {x, y}).call(() => {
                this.opacityTween(chongNode, false);
                resolve()
            }).start();
        })
    }

    private async onComplete(key: string, callback: Function) {
        try {
            await this.http.post('tasks/complete', {key}).then(({message}) => {
                this.showToast(message)
            })
        } catch (e) {

        }
        callback(false)
    }
}
